import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录处理
 * 
 * @author liyong
 * @version 1.0
 */
public class Login extends HttpServlet {

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.
	 * HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		String name = req.getParameter("name");
		boolean judge = true;
		System.out.println(name);
		Cookie[] cookies = req.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("name")) {
					cookie.setValue(name);
					judge = false;
				}
			}
		}
		/**
		 * 这里我改了，如果按照老师给的代码，cookie一旦设定，无论如何都无法重新登陆，也无法添加用户。
		 * 这里设定判断逻辑，如果之前有Cookie，用户又重新登陆了，这里以下的代码就会删除之前删除的cookie，以实现更新账户。
		 * 这样设置更符合常理。
		 */
		if(!judge) {
			for(Cookie cookie: cookies) {
				cookie.setMaxAge(0);
			}
		}
		Cookie newCookie = new Cookie("name", name);
		// 不设置的话，则cookies不写入硬盘,而是写在内存,只在当前页面有用,以秒为单位
		newCookie.setMaxAge(24 * 60 * 60);
		res.addCookie(newCookie);


		PrintWriter out = res.getWriter();
		res.setContentType("text/html");

		out.println("<html>");
		out.println("<title>");
		out.println("User list");
		out.println("</title>");
		out.println("<body><h4>Auto Login user added.</h4><hr><br><br>");
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("name")) {
					out.print(cookie.getValue());
				}
			}
		}
		out.println("<hr><br><br>");
		//这里增加了一个超链接，发送Get请求。
		out.println("<a href=\"Login.do\">click here to test auto login</a>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}

	/**
	 * 通过浏览器输入地址进入。
	 * 若有cookie存在，显示自动登陆界面，否则跳转到登录界面。
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Cookie[] cookies = req.getCookies();
		String name = null;
		String password = null;
		if(cookies != null) {
			for(Cookie cookie: cookies) {
				if(cookie.getName().equals("name")){
					name = cookie.getValue();
				} else if (cookie.getName().equals("password")) {
					password = cookie.getValue();
				}
			}
		}
		if(name!=null) {
				PrintWriter out = resp.getWriter();
				resp.setContentType("text/html");

				out.println("<html>");
				out.println("<title>");
				out.println("User list");
				out.println("</title>");
				out.println("<body><h4>Auto Login</h4><hr><br><br>");
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals("name")) {
						out.print(cookie.getValue());
					}
				}
				out.println("<br><br>");
				out.println("<a href=\"UserLogin.html\">click here to add users</a>");
				out.println("</body>");
				out.println("</html>");
				out.close();
		} else {
			resp.sendRedirect("/exec2_2/UserLogin.html");
		}


	}
}